/*
============================================================================
 Name        : fTimer.cpp
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : CfTimer implementation
============================================================================
*/

#include "fTimer.h"


fTimer * fTimer::NewL( const TInt aPriority, fTimerObserver & aTimeOutNotify, int aTimerType )
{
	fTimer * self = fTimer::NewLC( aPriority, aTimeOutNotify, aTimerType );
	CleanupStack::Pop();
	return self;
}


fTimer * fTimer::NewLC( const TInt aPriority, fTimerObserver & aTimeOutNotify, int aTimerType )
{
	fTimer * self = new( ELeave )fTimer( aPriority, aTimeOutNotify, aTimerType );
	CleanupStack::PushL( self );
	self->ConstructL();
	return self;
}


fTimer::fTimer( const TInt aPriority, fTimerObserver & aTimeOutNotify, int aTimerType ) :
    CTimer( aPriority ), iNotify( aTimeOutNotify ), iTimerType( aTimerType )
{
}

fTimer::~fTimer()
{
	if ( IsActive() )
	{
		Cancel();
	}
	else
	{
		DoCancel();
	}
}


void fTimer::ConstructL()
{
	CTimer::ConstructL();
	CActiveScheduler::Add( this );
	iLastActiveTime = 0;
}

void fTimer::After(TTimeIntervalMicroSeconds32 anInterval)
{
	iLastActiveTime.HomeTime();
	CTimer::After(anInterval);
}

void fTimer::DoCancel()
{
	iLastActiveTime = 0;
	CTimer::DoCancel();
}

void fTimer::RunL()
{
	iNotify.TimerExpired(iTimerType);
}

TTimeIntervalMicroSeconds fTimer::ActiveTime()
{
    if ( iLastActiveTime.Int64(  ) == 0 )
    {
        return TTimeIntervalMicroSeconds( 0 );
    }
    if ( !IsActive(  ) )
    {
        return TTimeIntervalMicroSeconds( 0 );
    }
	TTime time;
	time.HomeTime();
	return time.MicroSecondsFrom(iLastActiveTime);
}
